package com.kclm.se.jcf;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/*****************
 * <p>TreeSet的案例</p>
 * @author yejf
 * @since JDK8.0
 * @CreateDate 2020年8月8日 下午4:23:34 
 * @description 此类用来描述了xxxxx
 *
 */
public class TreeSetDemo {

	public static void main(String[] args) {
		TreeSet<Integer> intSet = new TreeSet<>();
		//添加对象, 注：此处我们存放的Integer类型是实现了 Comparable接口的
		intSet.add(10);
		intSet.add(9);
		intSet.add(18);
		intSet.add(7);
		intSet.add(10);  //与第一个元素是重复的，我们来看看结果如何
		//
		System.out.printf("集合中元素的个数：%d\n",intSet.size()); // 4, 不是 5
		
		System.out.println("--- 迭代的结果是：");
		//迭代出来
		for(Integer i : intSet) {
		    System.out.println(i);
		}
		
		//再创建一个 TreeSet
		TreeSet<Student> stuSet = new TreeSet<>();
		//创建Student对象
		Student s1 = new Student("jack", LocalDate.of(1998,12,12));
		Student s2 = new Student("peter", LocalDate.of(1997,5,22));
		Student s3 = new Student("ann", LocalDate.of(1998,9,18));
		Student s4 = new Student("solo", LocalDate.of(2000,1,1));
		Student s33 = new Student("ann", LocalDate.of(1998,9,18));
		Student s5 = new Student("ann", LocalDate.of(1997,10,18));
		Student s6 = new Student("ann", LocalDate.of(1999,6,18));
		//
		System.out.println(s3 == s33);
		//添加到集合中
		stuSet.add(s1);
		stuSet.add(s2);
		stuSet.add(s3);
		stuSet.add(s4);
		stuSet.add(s33);
		stuSet.add(s5);
		stuSet.add(s6);
		//
		System.out.printf("集合中元素的个数：%d\n",stuSet.size()); // 4, 不是 5
		//
		Iterator<Student> iter = stuSet.iterator();
		while(iter.hasNext()) {
			Student s = iter.next();
			System.out.println(s);
		}
		
		System.out.println("------------- 分割线 -------------");
		//再来创建一个TreeSet, 传入一个比较器
		TreeSet<Teacher> teaSet = new TreeSet<Teacher>(new Comparator<Teacher>() {
			//重写方法
			@Override
			public int compare(Teacher o1, Teacher o2) {
				//定义你自己的比较规则
				System.out.println("-- 调用了 Comparator接口实现类的 comparTo 方法...");
				return o1.getName().compareTo(o2.getName());
			};
		});
		
		//模拟三个Teacher实例
		Teacher t1 = new Teacher("张老师");
		Teacher t2 = new Teacher("叶老师");
		Teacher t3 = new Teacher("王老师");
		Teacher t4 = new Teacher("李老师");
		//
		teaSet.add(t1);
		teaSet.add(t2);
		teaSet.add(t3);
		teaSet.add(t4);
		//
		System.out.printf("集合中元素的个数：%d\n",teaSet.size()); // 4, 不是 5
		//
		for(Teacher tea : teaSet) {
			System.out.println(tea);
		}
	}
}
